// pages/createPatient/other.js
const api = require("./../../config/api")
const request = require("./../../utils/request")
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		allergy: '',
		otherAllergy: '',
		isOther: true,
		isEdit: '',
		patientData: {
			age: '',
			allergy: "",
			birthday: "",
			contact: "",
			contactRelationType: "",
			contactTel: "",
			familyHistory: "",
			idCard: "",
			nation: "",
			pastHistory: "",
			patientName: "",
			personalHistory: "",
			postnatalFactors: null,
			prenatalFactors: null,
			productionFactors: null,
			relation: "",
			sex: ''
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		let data = app.globalData.changePatientInfo

		let otherAllergy = this.data.otherAllergy
		let isOther = this.data.isOther
		if (data.allergy !== '无' && data.allergy !== '青霉素' && data.allergy !== '磺胺' && data.allergy !==
			'链霉素' && options.isEdit !== '0') {
			otherAllergy = data.allergy
			data.allergy = "其他"
			isOther = false
		}

		this.setData({
			patientData: data,
			isEdit: options.isEdit,
			isOther: isOther,
			otherAllergy: otherAllergy
		})
	},
	allergyChange(e) {
		let isOther = this.data.isOther
		let other = this.data.otherAllergy
		if (e.detail.value === '其他') {
			isOther = false
		} else {
			isOther = true
			other = ''
		}
		let patientData = this.data.patientData
		patientData.allergy = e.detail.value
		this.setData({
			patientData: patientData,
			isOther: isOther,
			otherAllergy: other
		})
	},
	otherAllergy(e) {
		this.setData({
			otherAllergy: e.detail.value
		})
	},
	pastHistory(e) {
		let patientData = this.data.patientData
		patientData.pastHistory = e.detail.value
		this.setData({
			patientData: patientData
		})
	},
	personalHistory(e) {
		let patientData = this.data.patientData
		patientData.personalHistory = e.detail.value
		this.setData({
			patientData: patientData
		})
	},
	familyHistory(e) {
		let patientData = this.data.patientData
		patientData.familyHistory = e.detail.value
		this.setData({
			patientData: patientData
		})
	},
	submit: async function() {
		let patientData = this.data.patientData
		if (!patientData.allergy) {
			wx.showToast({
				title: '请选择药物过敏史',
				icon: "none",
			})
			return
		}
		const userId = wx.getStorageSync('userId')
		if (patientData.allergy === '其他') {
			if (this.data.otherAllergy) {
				patientData.allergy = this.data.otherAllergy
			} else {
				wx.showToast({
					title: '请输入药物过敏史',
					icon: "none",
				})
				return
			}

		}
		patientData.birthday = patientData.birthday + ' 00:00:00'
		const today = new Date();
		const thisYear = today.getFullYear()
		const choseday = patientData.birthday.split('-')
		patientData.age = thisYear - choseday[0]
		if (this.data.isEdit === '0') {

			console.log(patientData.age)
			patientData.appletUserId = userId
			const res = await request.post(api.savePatient, patientData)
			if (res.httpStatus === 200) {
				wx.showToast({
					title: '新建成功',
				})
				setTimeout(function() {
					wx.navigateBack({
						delta: 4
					})
				}, 1000)
				// this.bindPatient(res.data)
			}
		} else {
			const res = await request.post(api.updatePatient, patientData)
			wx.showToast({
				title: '修改成功',
			})
			setTimeout(function() {
				wx.navigateBack({
					delta: 4
				})
			}, 1000)

		}


	},
	bindPatient: async function(id) {
		const userId = wx.getStorageSync('userId')
		const data = {
			userId: userId,
			patientId: id

		}
		const res = await request.post(api.bindPatient, data)
		if (res.httpStatus === 200) {
			wx.showToast({
				title: '绑定成功',
			})
			setTimeout(function() {
				wx.navigateBack({
					delta: 4
				})
			}, 1000)
		}
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
